package com.tarena.lbs.user.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tarena.lbs.user.pojo.po.UserPO;
import com.tarena.lbs.user.pojo.query.UserQuery;
import com.tarena.lbs.user.pojo.vo.TagListVO;
import com.tarena.lbs.user.pojo.vo.UserVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;


@Mapper
public interface UserMapper extends BaseMapper<UserPO> {
    @Select("<script>" +
            "select count(*) from lbs_user " +
            "<where>" +
            "   <if test='userName != null and userName != \"\"'>" +
            "       and user_name like concat('%', #{userName}, '%')" +
            "   </if>" +
            "   <if test='phone != null and phone != \"\"'>" +
            "       and phone = #{phone}" +
            "   </if>" +
            "   <if test='providerType != null'>" +
            "       and provider_type = #{providerType}" +
            "   </if>" +
            "   <if test='regTime != null and regTime.size() == 2'>" +
            "       and reg_time between #{regTime[0]} and #{regTime[1]}" +
            "   </if>" +
            "</where>" +
            "</script>")
    Long selectUserCount(UserQuery query);

    @Select("<script>" +
            "select " +
            "id, user_name as userName, password, nick_name as nickName, " +
            "ip_address as ipAddress, reg_time as regTime, last_log_time as lastLogTime, " +
            "introduction, phone, provider_type as providerType, status, " +
            "union_id as unionId " +
            "from lbs_user " +
            "<where>" +
            "   <if test='userName != null and userName != \"\"'>" +
            "       and user_name like concat('%', #{userName}, '%')" +
            "   </if>" +
            "   <if test='phone != null and phone != \"\"'>" +
            "       and phone = #{phone}" +
            "   </if>" +
            "   <if test='providerType != null'>" +
            "       and provider_type = #{providerType}" +
            "   </if>" +
            "   <if test='regTime != null and regTime.size() == 2'>" +
            "       and reg_time between #{regTime[0]} and #{regTime[1]}" +
            "   </if>" +
            "</where>" +
            "order by reg_time desc" +
            "</script>")
    List<UserVO> selectUserList(UserQuery query);

    @Select("SELECT t.id, t.rule_name AS ruleName " +
            "FROM lbs_user_tag_rules t " +
            "INNER JOIN lbs_user_tags ut ON t.id = ut.tag_id " +  // 关联用户-标签中间表
            "WHERE ut.user_id = #{userId} " +                // 通过userId查询关联的tag
            "ORDER BY t.id ASC")
    List<TagListVO> bindUserTags(Integer id);

    @Select("SELECT " +
            "    DATE(reg_time) AS regDate,  " +
            "    COUNT(id) AS userCount  " +
            "FROM " +
            "    lbs_user " +
            "WHERE reg_time IS NOT NULL " +
            "GROUP BY " +
            "    DATE(reg_time) " +
            "ORDER BY " +
            "    regDate ASC;")
    List<Map<String, Object>> getRegisterCount();
}
